package com.atguigu.cart.controller;

import com.atguigu.cart.service.ICartService;
import com.atguigu.cart.vo.CartItemVO;
import com.atguigu.cart.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author : wenchao.long
 * @date : Created in 2021/8/16 14:50 周一
 * @description :
 */
@Controller
@Slf4j
@AllArgsConstructor
@Api("购物车")
public class CartController {

    private final ICartService cartService;

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVO> getCurrentUserCartItems() {
        return cartService.getUserCartItems();
    }

    @ApiOperation("删除购物项")
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @ApiOperation("修改购物项数量")
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.changItemCount(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @ApiOperation("购物车的选中状态")
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.gulimall.com/cart.html";

    }

    @ApiOperation("获取购物车信息")
    @GetMapping("/cart.html")
    public String cartListPage(Model model) {
        /*
        浏览器有一个cookie user-key 表示用户身份 一个月后过期
        如果第一次使用jd的购物车功能 都会给一个临时用户身份
        浏览器以后保存 每次访问都会带上这个cookie

        登录：session有
        没登录：按照cookie里面带来user-key来做
        第一次：如果没有临时用户，帮忙创建一个临时用户

        采用拦截器实现这个方法
         */

        // 1. 快速得到 用户信息
        //UserInfoDTO userInfoDTO = CartInterceptor.threadLocal.get();
        //log.info(userInfoDTO.toString());

        CartVO cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    @ApiOperation("添加商品到购物车")
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) {
        cartService.addToCart(skuId, num);
        ra.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccessPage.html";
    }

    @GetMapping("/addToCartSuccessPage.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,
                                       Model model) {
        // 重定向到成功页面 再次查询购物车数据即可
        CartItemVO cartItemVO = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItemVO);
        return "success";
    }

}
